package com.magus.project.yddemo.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.system.annotation.AppFunctionInfo;
import com.magus.framework.system.annotation.AppFunctionInfo.FunctionType;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.framework.system.entity.Company;
import com.magus.framework.system.service.CompanyService;
import com.magus.project.yddemo.entity.Indicator;
import com.magus.project.yddemo.entity.IndicatorData;
import com.magus.project.yddemo.service.IndicatorDataService;
import com.magus.project.yddemo.webbean.IndicatorDataMonthResultBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 月评价结果
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Api(tags = "IndicatorDataMonthResultManager", description = "月评价结果")
@RestController
@RequestMapping("/yddemo/dataMonthResults")
@AppGroupInfo(key = "yddemo:dataMonthResult", name = "月评价结果", appKey = "yddemo")
public class IndicatorDataMonthResultController extends BaseController {

	@Autowired
	private IndicatorDataService indicatorDataService;

	@Autowired
	private CompanyService companyService;

	SimpleDateFormat month = new SimpleDateFormat("yyyy-MM");

	/**
	 * 查询集团环保指标月评价结果
	 * 
	 * @param date
	 *            日期
	 * @param companyId
	 *            电厂
	 * @return 指标数据集合
	 */
	@GetMapping
	@RequiresPermissions("yddemo:dataMonthResult:view")
	@ApiOperation(value = "查询集团环保指标月评价结果", notes = "查询集团环保指标月评价结果")
	@AppFunctionInfo(key = "yddemo:dataMonthResult:view", name = "查询集团环保指标月评价结果", groupKey = "yddemo:dataMonthResult", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/dataMonthResults/page/view")
	ResponseEntity<IndicatorDataMonthResultBean> findAll(@RequestParam(required = false) String date) {
		// 查询指标
		List<Indicator> indicatorList = indicatorDataService.getIndicatorList();
		// 查询电厂
		List<Company> companyList = companyService.findAll();
		// 查询集团表YDDEMO_INDICATOR_DATA
		Date bDate = new Date();
		try {
			bDate = month.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		List<SearchFilter> searchFilters = new ArrayList<>();
		// searchFilters.add(SearchFilter.equal("indicatorDetail.company.id",
		// companyId));
//		searchFilters.add(SearchFilter.like("indicatorDetail.comUnit.name", "燃煤"));
//		searchFilters.add(SearchFilter.like("indicatorDetail.indicator.major.name", "环保"));
		searchFilters.add(SearchFilter.equal("time", bDate.getTime()));
		searchFilters.add(SearchFilter.equal("dimension", "month"));
		searchFilters.add(SearchFilter.like("indicatorDetail.indicator.dimension", "month"));
		searchFilters.add(SearchFilter.equal("indicatorDetail.dimension", "month"));

		Specification<IndicatorData> spec = buildFilterRequest(searchFilters);
		List<IndicatorData> dataList = indicatorDataService.findAll(spec);

		List<IndicatorData> result = indicatorDataService.getIndicatorDataList(indicatorList, companyList, dataList);

		IndicatorDataMonthResultBean bean = new IndicatorDataMonthResultBean();
		bean.setCompanyList(companyList);
		bean.setIndicatorList(indicatorList);
		bean.setResult(result);

		return ResponseEntity.ok(bean);
	}

}
